<template>
  <div class="app-container">
    <!-- 搜索 -->
    <div v-if="showSearch" class="filter-container">
      <el-form :inline="true" :model="listQuery" class="form-inline">
        <el-form-item label="">
          <el-select v-model="listQuery.countDate" placeholder="创建时间" default-first-option size="small">
            <el-option label="全部" value="all" />
            <el-option label="昨天" value="yesterday" />
            <el-option label="今天" value="today" />
            <el-option label="本周" value="week" />
            <el-option label="本月" value="month" />
            <el-option label="本季度" value="quarter" />
            <el-option label="本年" value="year" />
            <el-option label="自定义" value="custom" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="listQuery.countDate === 'custom'" label="">
          <el-date-picker
            v-model="dateTime"
            :picker-options="pickerOptions"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
            align="right"
            clearable
            size="small"
          />
        </el-form-item>
        <el-form-item>
          <el-button v-waves type="primary" icon="el-icon-search" size="small" @click="handleFilter">
            搜索
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-waves type="warning" icon="el-icon-refresh" size="small" @click="handleFilterClear">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 操作 -->
    <el-row style="margin-bottom: 10px;">
      <el-col :xs="24" :sm="24" :lg="24">
        <el-tooltip content="刷新" placement="top">
          <el-button v-waves type="warning" icon="el-icon-refresh" circle @click="handleFilterClear" />
        </el-tooltip>
        <el-tooltip content="搜索" placement="top">
          <el-button v-waves type="primary" icon="el-icon-search" circle @click="showSearch = !showSearch" />
        </el-tooltip>
      </el-col>
    </el-row>

    <!-- 表格 -->
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      @sort-change="sortChange"
      style="width: 100%;"
    >
      <el-table-column label="ID" align="center" width="100" prop="id" sortable="custom">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="统计日期" width="150px" prop="countDate" sortable="custom">
        <template slot-scope="scope">
          <span>{{ scope.row.countDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="Total" min-width="150px" prop="total" sortable="custom">
        <template slot-scope="scope">
          <span>{{ scope.row.total }}</span>
        </template>
      </el-table-column>
      <el-table-column label="Human" width="100px" prop="human" sortable="custom">
        <template slot-scope="scope">
          <span>{{ scope.row.human }}</span>
        </template>
      </el-table-column>
      <el-table-column label="Google" width="100px" prop="google" sortable="custom">
        <template slot-scope="scope">
          <span>{{ scope.row.google }}</span>
        </template>
      </el-table-column>
      <el-table-column label="Bing" width="100px" prop="bing" sortable="custom">
        <template slot-scope="scope">
          <span>{{ scope.row.bing }}</span>
        </template>
      </el-table-column>
      <el-table-column label="Baidu" width="100px" prop="baidu" sortable="custom">
        <template slot-scope="scope">
          <span>{{ scope.row.baidu }}</span>
        </template>
      </el-table-column>
      <el-table-column label="Sogou" width="100px" prop="sogou" sortable="custom">
        <template slot-scope="scope">
          <span>{{ scope.row.sogou }}</span>
        </template>
      </el-table-column>
      <el-table-column label="Yisou" width="100px" prop="yisou" sortable="custom">
        <template slot-scope="scope">
          <span>{{ scope.row.yisou }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="80px" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-tooltip content="查看详情" placement="top">
            <el-button v-waves type="primary" icon="el-icon-view" circle @click="handleLook(scope.$index,scope.row.countDate)"/>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination v-show="total>0" :current-page="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.psize" :total="total" background layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>
  </div>
</template>

<script>
import { getList } from '@/api/visits'
import waves from '@/directive/waves'
import { pickerOptions } from '@/utils'

export default {
  name: 'Visits',
  components: { },
  directives: {
    waves
  },
  filters: {
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: null,
      listLoading: true,
      showSearch: false,
      listQuery: {
        page: 1,
        psize: 10,
        sort: 'id desc',
        countDate: '',
        startTime: '',
        endTime: ''
      },
      dateTime: '',
      pickerOptions: pickerOptions
    }
  },
  watch: {
    dateTime: function(val) {
      this.listQuery.startTime = val[0]
      this.listQuery.endTime = val[1]
    }
  },
  created() {
    this.fetchList()
  },
  methods: {
    fetchList() {
      this.listLoading = true
      getList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.fetchList()
    },
    handleFilterClear() {
      this.listQuery = {
        page: 1,
        psize: 10,
        sort: 'id desc',
        countDate: '',
        startTime: '',
        endTime: ''
      }
      this.dateTime = ''
      this.fetchList()
    },
    handleSizeChange(val) {
      this.listQuery.psize = val
      this.fetchList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.fetchList()
    },
    // 排序
    sortChange(data) {
      const { prop, order } = data
      if (prop !== null) {
        this.listQuery.sort = prop + ' ' + (order === 'ascending' ? 'asc' : 'desc')
        this.fetchList()
      }
    },
    // 查看详情
    handleLook(index, countDate) {
      this.$router.push({
        path: '/log/visit',
        params: {
          visitTime: 'custom',
          startTime: countDate,
          endTime: countDate
        }
      })
    }
  }
}
</script>
